package com.nt.thah.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.beust.jcommander.internal.Lists;
import com.github.pagehelper.PageHelper;
import com.nt.thah.constant.SysCode;
import com.nt.thah.constant.WebConfig;
import com.nt.thah.dao.ProductAttrDao;
import com.nt.thah.dao.ProductDao;
import com.nt.thah.entity.*;
import com.nt.thah.enums.ResultCode;
import com.nt.thah.exception.BusinessException;
import com.nt.thah.requestparam.BaseParam;
import com.nt.thah.requestparam.ProductParam;
import com.nt.thah.service.CommonService;
import com.nt.thah.service.ProductService;
import com.nt.thah.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description: 登录服务
 * @author: ls
 * @Date: 2020/7/10 14:00
 */
@Slf4j
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductDao productDao;

    @Resource
    private ProductAttrDao productAttrDao;

    @Resource
    private CommonService commonServiceImpl;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private AsyncUtils asyncUtils;

    @Resource
    private WebConfig webConfig;

    @Resource
    private OssUtils ossUtils;

    /**
     * @Description: 获取产品列表
     * @Author: ls
     * @Date: 2020/08/04 17:49
     */
    @Override
    public Map<String, Object> getList(BaseParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<Product> productList = productDao.getList(param);
        return commonServiceImpl.buildQryResultMap(productList);
    }

    /**
     * @Description: 获取产品详情
     * @Author: ls
     * @Date: 2020/08/04 17:56
     */
    @Override
    public JSONObject getProductDetail(Integer productId) {
        return commonServiceImpl.getProductInfo(productId);
    }

    /**
     * @Description： 保存产品
     * @Author: ls
     * @Date: 2020/11/13 9:55 上午
     */
    @Override
    public void save(ProductParam param) {
        checkRepeat(param);
        Product product = Product.builder().build();
        BeanUtils.copyProperties(param, product);
        productDao.save(product);
        saveProductAttr(param, product.getId());
    }

    /**
     * @Description: 更新产品信息
     * @Author: ls
     * @Date: 2020/08/04 18:14
     */
    @Override
    public void update(ProductParam param) {
        checkRepeat(param);
        Product product = Product.builder().build();
        BeanUtils.copyProperties(param, product);
        productDao.update(product);
        productAttrDao.deleteProductAttr(param.getId());
        saveProductAttr(param, product.getId());
        asyncUtils.getFuture(() -> {
            String redisKey = String.format("%s%s", SysCode.REDIS_KEY_PRODUCT_DETAIL_INFO, param.getId());
            redisUtil.del(redisKey);
        });
    }

    /**
     * @Description： 保存产品属性
     * @Author: ls
     * @Date: 2020/11/13 11:16 上午
     */
    private void saveProductAttr(ProductParam param, Integer productId) {
        List<ProductAttr> attrList = Lists.newArrayList();
        attrList.add(ProductAttr.builder().productId(productId).key("inventory").value(param.getInventory()).name("库存").build());
        attrList.add(ProductAttr.builder().productId(productId).key("score").value(param.getScore()).name("积分").build());
        attrList.add(ProductAttr.builder().productId(productId).key("price").value(param.getPrice()).name("价格").build());
        attrList.add(ProductAttr.builder().productId(productId).key("logo").value(param.getLogo().replaceAll(webConfig.getOriginOssDomainName(), webConfig.getMyOssDomainName())).name("logo").build());
        attrList.add(ProductAttr.builder().productId(productId).key("head").value(param.getHead().replaceAll(webConfig.getOriginOssDomainName(), webConfig.getMyOssDomainName())).name("头图").build());
        attrList.add(ProductAttr.builder().productId(productId).key("detail").value(param.getDetail().replaceAll(webConfig.getOriginOssDomainName(), webConfig.getMyOssDomainName())).name("详情").build());
        productAttrDao.batchSave(attrList);
    }

    /**
     * @Description： 校验重复
     * @Author: ls
     * @Date: 2020/12/26 3:33 下午
     */
    private void checkRepeat(ProductParam param) {
        Integer record = productDao.checkRepeat(param);
        if (record != null) {
            throw new BusinessException(ResultCode.DATA_ALREADY_EXISTED);
        }
    }

}